package com.example.estest.controller;

import com.example.estest.util.ExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * <一句话描述功能>
 * <详细功能描述>
 *
 * @author: fei
 * @date: 2020/10/17
 */
@RestController("/")
public class ExportController {
    @GetMapping("/hello")
    public String hello() {
        return "hello world!";
    }


    @GetMapping("/export")
    public void download(HttpServletResponse response) throws IOException {
        ByteArrayOutputStream stream = ExcelUtil.createOne();
        response.setContentType(response.getContentType());
        response.setHeader("Content-disposition", "attachment; filename=person.xls");
        byte[] bytes = stream.toByteArray();
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        out.write(bytes);
        out.close();
        stream.close();
    }
}
